using System;
using System.ComponentModel;
using EveIntel.WpfApplication.Interfaces;

namespace EveIntel.WpfApplication.ViewModels
{
    /// <summary>
    /// ViewModel abstract base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ViewModelBase<T> : INotifyPropertyChanged where T : class, IView
    {
        private T _view;

        /// <summary>
        /// Initializes a new instance
        /// </summary>
        protected ViewModelBase()
        {
        }
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="view">The view associated</param>
        protected ViewModelBase(T view): this()
        {
            if(view ==null)throw new ArgumentNullException("view");
            _view = view;
            _view.DataContext = this;
        }
        /// <summary>
        /// Obtains or defines the view associated
        /// </summary>
        public T View
        {
            get { return _view; }
            set { _view = value; InvokePropertyChanged(new PropertyChangedEventArgs("View")); }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public virtual event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises the PropertyChanged event
        /// </summary>
        /// <param name="e"></param>
        internal virtual void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }
    }
}